//
//  WatchViewController.m
//  CarPark
//
//  Created by oceans on 2019/7/16.
//  Copyright © 2019 oceans. All rights reserved.
//

#import "WatchViewController.h"
#import "WatchCircleCollectionViewCell.h"
#import "WatchHeaderCollectionReusableView.h"
#import "WatchTextCollectionViewCell.h"
#import "EmptyCollectionReusableView.h"
#import "ParkDetailViewController.h"
#import "XiaoXiViewController.h"

@interface WatchViewController () <UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout, WatchHeaderViewPressedDelegate>

@property (strong, nonatomic) UICollectionView *collection;

@property (strong, nonatomic) NSMutableArray *array;
@property (assign, nonatomic) int park;
@property (assign, nonatomic) int car;
@end

@implementation WatchViewController

- (instancetype)init {
    
    self = [super init];
    if (self) {
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loadData) name:klogin_notification object:nil];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.array = [NSMutableArray array];
    [self setupView];
    [self loadData];
}

- (void)loadData {
    
    if ([UserModel shared].hasLogin == NO) {
        return;
    }
    defweakself
    [HttpRequest getURL:kmonitor paras:nil showProgress:NO successful:^(id  _Nullable result) {
        NSDictionary *dic = result[@"datas"];
        weakself.car = [dic[@"autocar_num"] intValue];
        weakself.park = [dic[@"parking_num"] intValue];
        [weakself.array addObjectsFromArray:dic[@"parkinges"]];
        [weakself.collection reloadData];
    } failed:^(int code, NSString * _Nullable errMsg) {
        
    }];
}

- (void)setupView {
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"监管中心";
    
    UIBarButtonItem *right = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"xiaoxi"] style:UIBarButtonItemStylePlain target:self action:@selector(messageButtonPressed)];
    self.navigationItem.rightBarButtonItem = right;
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    self.collection = [[UICollectionView alloc] initWithFrame:CGRectMake(0, XNavigationHeight, ScreenWidth, ScreenHeight-XNavigationHeight-XTabBarHeight) collectionViewLayout:layout];
    _collection.delegate = self;
    _collection.dataSource = self;
    _collection.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_collection];
    [_collection registerNib:[UINib nibWithNibName:@"WatchCircleCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"WatchCircleCollectionViewCell"];
    [_collection registerNib:[UINib nibWithNibName:@"WatchTextCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"WatchTextCollectionViewCell"];
    [_collection registerNib:[UINib nibWithNibName:@"WatchHeaderCollectionReusableView" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"WatchHeaderCollectionReusableView"];
    [_collection registerClass:[EmptyCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"EmptyCollectionReusableView"];
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    
    return _array.count + 1;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
    
    
    if (section == 0) {
        return CGSizeZero;
    }
    return CGSizeMake(ScreenWidth, 45.0);
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section {
    
    return CGSizeMake(ScreenWidth, 10.0);
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        
        WatchHeaderCollectionReusableView *view = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"WatchHeaderCollectionReusableView" forIndexPath:indexPath];
        view.titleLabel.text = _array[indexPath.section-1][@"name"];
        view.tag = indexPath.section-1;
        view.delegate = self;
        return view;
    }else {
        
        EmptyCollectionReusableView *view = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"EmptyCollectionReusableView" forIndexPath:indexPath];
        view.backgroundColor = COLOR(242, 242, 242, 1);
        return view;
    }
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    
    if (section == 0) {
        return UIEdgeInsetsZero;
    }
    return UIEdgeInsetsMake(15, 40, 15, 40);
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    
    return 0.0;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    
    if (section == 0) {
        return ScreenWidth/3.0;
    }
    return 40.0;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0) {
        return CGSizeMake(ScreenWidth/3.0, 70.0);
    }
    return CGSizeMake((ScreenWidth-40-40-40)*0.5, (ScreenWidth-40-40-40)*0.5);
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    if (section == 0) {
        return 2;
    }
    return 2;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0) {
        
        WatchTextCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"WatchTextCollectionViewCell" forIndexPath:indexPath];
        if (indexPath.item == 0) {
            NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%d\n", _park] attributes:@{NSFontAttributeName: [UIFont boldSystemFontOfSize:16.0], NSForegroundColorAttributeName: COLOR(51, 51, 51, 1)}];
            [string appendAttributedString:[[NSAttributedString alloc] initWithString:@"当前停车场" attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:13.0], NSForegroundColorAttributeName: COLOR(151, 151, 151, 1)}]];
            NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
            style.alignment = NSTextAlignmentCenter;
            style.lineSpacing = 13.0;
            [string addAttribute:NSParagraphStyleAttributeName value:style range:NSMakeRange(0, string.length)];
            cell.titleLabel.attributedText = string;
        }else {
            NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%d\n", _car] attributes:@{NSFontAttributeName: [UIFont boldSystemFontOfSize:16.0], NSForegroundColorAttributeName: COLOR(51, 51, 51, 1)}];
            [string appendAttributedString:[[NSAttributedString alloc] initWithString:@"当前车辆总数" attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:13.0], NSForegroundColorAttributeName: COLOR(151, 151, 151, 1)}]];
            NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
            style.alignment = NSTextAlignmentCenter;
            style.lineSpacing = 13.0;
            [string addAttribute:NSParagraphStyleAttributeName value:style range:NSMakeRange(0, string.length)];
            cell.titleLabel.attributedText = string;
        }
        return cell;
    }
    WatchCircleCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"WatchCircleCollectionViewCell" forIndexPath:indexPath];
    NSDictionary *dic = _array[indexPath.section-1];
    if (indexPath.item == 0) {
        NSMutableAttributedString *string = nil;
        if ([dic[@"capture_nums_static"][@"error_num"] intValue] == 0) {
            string = [[NSMutableAttributedString alloc] initWithString:@"\n正常" attributes:@{NSFontAttributeName: [UIFont boldSystemFontOfSize:16.0], NSForegroundColorAttributeName: COLOR(51, 51, 51, 1)}];
        }else {
            string = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"\n%d异常", [dic[@"capture_nums_static"][@"error_num"] intValue]] attributes:@{NSFontAttributeName: [UIFont boldSystemFontOfSize:16.0], NSForegroundColorAttributeName: [UIColor redColor]}];
        }
        [string insertAttributedString:[[NSAttributedString alloc] initWithString:@"捕捉设备" attributes:@{NSFontAttributeName: [UIFont boldSystemFontOfSize:13.0], NSForegroundColorAttributeName: COLOR(151, 151, 151, 1)}] atIndex:0];
        NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
        style.alignment = NSTextAlignmentCenter;
        style.lineSpacing = 13.0;
        [string addAttribute:NSParagraphStyleAttributeName value:style range:NSMakeRange(0, string.length)];
        cell.titleLabel.attributedText = string;
        [cell clear];
        
        float error = [dic[@"capture_nums_static"][@"error_num"] floatValue];
        float all = [dic[@"capture_nums_static"][@"all_num"] floatValue];
        if (error == 0) {
            [cell stroke:2*M_PI color:COLOR(94, 156, 211, 1)];
        }else {
            float err = error/all;
            [cell stroke:(1.0-err)*2*M_PI color:COLOR(26, 175, 75, 1)];
            [cell stroke:err*2*M_PI color:[UIColor redColor]];
        }
    }else {
        
        NSMutableAttributedString *string = nil;
        //string = [[NSMutableAttributedString alloc] initWithString:@"\n无数据" attributes:@{NSFontAttributeName: [UIFont boldSystemFontOfSize:16.0], NSForegroundColorAttributeName: COLOR(51, 51, 51, 1)}];
        if ([dic[@"rfid_nums_static"][@"error_num"] intValue] == 0) {
            string = [[NSMutableAttributedString alloc] initWithString:@"\n正常" attributes:@{NSFontAttributeName: [UIFont boldSystemFontOfSize:16.0], NSForegroundColorAttributeName: COLOR(51, 51, 51, 1)}];
        }else {
            string = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"\n%d异常", [dic[@"rfid_nums_static"][@"error_num"] intValue]] attributes:@{NSFontAttributeName: [UIFont boldSystemFontOfSize:16.0], NSForegroundColorAttributeName: [UIColor redColor]}];
        }
        [string insertAttributedString:[[NSAttributedString alloc] initWithString:@"车辆芯片" attributes:@{NSFontAttributeName: [UIFont boldSystemFontOfSize:13.0], NSForegroundColorAttributeName: COLOR(151, 151, 151, 1)}] atIndex:0];
        NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
        style.alignment = NSTextAlignmentCenter;
        style.lineSpacing = 13.0;
        [string addAttribute:NSParagraphStyleAttributeName value:style range:NSMakeRange(0, string.length)];
        cell.titleLabel.attributedText = string;
        [cell clear];
        
        float error = [dic[@"rfid_nums_static"][@"error_num"] floatValue];
        float all = [dic[@"rfid_nums_static"][@"all_num"] floatValue];
        if (error == 0) {
            [cell stroke:2*M_PI color:COLOR(155, 155, 155, 1)];
        }else {
            float err = error/all;
            [cell stroke:(1.0-err)*2*M_PI color:COLOR(26, 175, 75, 1)];
            [cell stroke:err*2*M_PI color:[UIColor redColor]];
        }
    }
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    
}

- (void)headerViewPressed:(NSInteger)tag {
    
    ParkDetailViewController *con = [[ParkDetailViewController alloc] init];
    con._id = [_array[tag][@"id"] intValue];
    [Utility pushViewController:con];
}

- (void)messageButtonPressed {
    
    XiaoXiViewController *con = [[XiaoXiViewController alloc] init];
    [Utility pushViewController:con];
}

@end
